package moodish.comm;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Vector;



/**
 * @author Grupo26
 * @version 0.1
 */

public class ServerCommClass implements ServerComm {
	
	private int PORTA = 0;
	private ServerSocket serverSocket;
    private static Vector ClientSockets;
	private static Vector NickNames;
	
	public ServerCommClass(int port) throws IOException {
		
		
		PORTA = port;
		
	}
		
	@Override
	public void start(){
		
		
		try {
			serverSocket = new ServerSocket(PORTA);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		ClientSockets = new Vector();
		NickNames = new Vector();
		
		while(true)
	      {
	         try
	         {
	        	
	        	Socket clientSocket = serverSocket.accept();
	 			Client client = new Client(clientSocket);
	 			
	         }catch(SocketTimeoutException s){
	            System.out.println("Socket timed out!");
	            break;
	         }catch(IOException e){
	        	System.out.println("I/O error: " + e);
	         } catch (Exception e){
				e.printStackTrace();
			}
	         
	      }


	      
	}

	@Override
	public ServerSideMessage getNextMessage() {
		
		return null;
	}

	@Override
	public boolean hasNextMessage() {
		
		boolean ret = false;
		
		return ret;
	}

	@Override
	public void sendMoodishMessage(String fromNicename, String toNickname, String moodishMessage) {
		
		
	}

	@Override
	public void sendNewFriendship(String toNickname, String newFriendship) {
		
		
	}

	@Override
	public void sendNewUnfriendship(String toNickname, String unfriendship) {
		
		
	}

	@Override
	public void sendError(String toNickname, String error) {
		
		
	}

	@Override
	public void sendClientConnected(String toNickname, String connectedNickname) {
		
		
	}

	@Override
	public void sendClientDisconnected(String toNickname, String disconnectedNickname) {
		
		
	}

	@Override
	public boolean clientIsConnected(String nickname) {
		
		boolean ret = false;
		return ret;
		
	}

	@Override
	public void disconnectClient(String nickname) {
		
		
	}
    
	
	
	
	
	
	
	
	
	
	
	public class Client extends Thread {
		
		protected Socket socket;
		protected String nickName;
		ObjectInputStream dIn;

		public Client(Socket clientSocket) throws Exception {
			
			socket = clientSocket;
			dIn= new ObjectInputStream(socket.getInputStream());
			ServerSideMessageClass ssm = (ServerSideMessageClass)dIn.readObject();  
			
			nickName = ssm.getClientNickname();
			NickNames.add(nickName);
			ClientSockets.add(socket);	
			
			System.out.println( "\n Client conected: " + nickName);
			
			start();
			
		}
	    
		public void run() {
			  
			try{ 
			
				while (true) {
					
					try
					{
						
						ServerSideMessageClass ssm = (ServerSideMessageClass)dIn.readObject();  
						String msgFromClient = ssm.getPayload();
						
						int iCount=0;
					    
						if(ssm.getType()==ServerSideMessage.Type.CLIENT_DISCONNECTED){
							
							break;
			
						}else{
							
							for(iCount=0;iCount<NickNames.size();iCount++)
							{
								if(!NickNames.elementAt(iCount).equals(nickName))
								{	
									
									Socket tSoc=(Socket)ClientSockets.elementAt(iCount);
									ClientSideMessageClass cm = new ClientSideMessageClass(msgFromClient,"",ClientSideMessage.Type.MOODISH_MESSAGE);
									ObjectOutputStream out = new ObjectOutputStream(tSoc.getOutputStream());
									out.writeObject(cm);
									out.flush();
												
								}
							}
						
						}
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
				}
				
			}finally{
				
				try{
					dIn.close();
					socket.close();
				}catch(IOException ioException){
					ioException.printStackTrace();
				}
				
			}
	        
	    }
		
	}
	}
